<template>
    <div class="header arch">
        <div class="mobile">
            <div class="logo">
                <el-image
                    :src="require('../../assets/img/logo.png')"
                    fit="scale-down"></el-image>
            </div>
            <h1>CloudNativeFrontend</h1>
            <el-menu :default-active="activeIndex" class="el-menu-header right" @select="handleSelect">
                <el-submenu index="8" class="nav-avtar">
                    <template slot="title">
                        <div>
                            <el-avatar icon="el-icon-user-solid" style="margin-right: 5px;"></el-avatar>
                            {{ username }} - {{ rolename }}
                            <!-- <el-avatar> {{ username.substring(0, 2) }} </el-avatar> -->
                        </div>
                    </template>
                    <el-menu-item index="8-1" @click="gotoPage('userpwd')">修改密码</el-menu-item>
                    <el-menu-item index="8-2" @click="gotoPage('login')">退出登录</el-menu-item>
                </el-submenu>
                <el-menu-item index="1" @click="gotoPage('dashboard')">控制中心</el-menu-item>
            </el-menu>
            <el-menu :default-active="activeIndex" class="el-menu-header right" @select="handleSelect">
                <el-submenu :index="'' + index" v-for="(item, index) in permissions">
                    <template slot="title">{{ item.name }}</template>
                    <el-menu-item :index="index + '-' + indexL2" @click="gotoPage(itemL2.path)" v-for="(itemL2, indexL2) in item.children">{{ itemL2.name }}</el-menu-item>
                </el-submenu>
            </el-menu>
        </div>
        <div class="desktop">
            <div class="header-bg"></div>
            <div class="title-bg">
                <div class="shape"></div>
            </div>
            <div class="menu-group left">
                <el-menu :default-active="activeIndex" class="el-menu-header left" mode="horizontal" @select="handleSelect">
                    <el-menu-item index="1" @click="gotoPage('dashboard')">控制中心</el-menu-item>
                </el-menu>
                <el-menu :default-active="activeIndex" class="el-menu-header right" mode="horizontal" @select="handleSelect">
                    <el-submenu :index="'' + index" v-for="(item, index) in permissions.slice(0, Math.ceil(permissions.length / 2))">
                        <template slot="title">{{ item.name }}</template>
                        <el-menu-item :index="index + '-' + indexL2" @click="gotoPage(itemL2.path)" v-for="(itemL2, indexL2) in item.children">{{ itemL2.name }}</el-menu-item>
                    </el-submenu>
                </el-menu>
            </div>
            <div class="center-title">
                <el-image
                    :src="require('../../assets/img/logo.png')"
                    fit="cover"></el-image>
                <h1>CloudNativeFront</h1>
            </div>
            <div class="menu-group right">
                <el-menu :default-active="activeIndex" class="el-menu-header right" mode="horizontal" @select="handleSelect">
                    <el-submenu :index="'' + index" v-for="(item, index) in permissions.slice(Math.ceil(permissions.length / 2), permissions.length)">
                        <template slot="title">{{ item.name }}</template>
                        <el-menu-item :index="index + '-' + indexL2" @click="gotoPage(itemL2.path)" v-for="(itemL2, indexL2) in item.children">{{ itemL2.name }}</el-menu-item>
                    </el-submenu>
                </el-menu>
                <el-menu :default-active="activeIndex" class="el-menu-header right" mode="horizontal" @select="handleSelect">
                    <!-- <el-submenu index="6" v-if="username === 'admin' || rolename === '系统管理员'">
                        <template slot="title">用户中心</template>
                        <el-menu-item index="6-1" @click="gotoPage('users')"  v-if="applyForPermission('用户查看')">用户管理</el-menu-item>
                        <el-menu-item index="6-2" @click="gotoPage('roles')"  v-if="applyForPermission('角色查看')">角色管理</el-menu-item>
                        <el-menu-item index="6-3" @click="gotoPage('permissions')" v-if="applyForPermission('权限查看')">权限管理</el-menu-item>
                    </el-submenu> -->
                    <el-submenu index="8" class="nav-avtar">
                        <template slot="title">
                            <div>
                                <el-avatar icon="el-icon-user-solid"></el-avatar>
                                <!-- <el-avatar> {{ username.substring(0, 2) }} </el-avatar> -->
                            </div>
                        </template>
                        <el-menu-item index="8-1">
                            {{ username }}
                            -
                            {{ rolename }}
                        </el-menu-item>
                        <el-divider></el-divider>
                        <el-menu-item index="8-2" @click="gotoPage('userpwd')">修改密码</el-menu-item>
                        <el-menu-item index="8-3" @click="gotoPage('login')">退出登录</el-menu-item>
                    </el-submenu>
                    <!-- <el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item> -->
                </el-menu>
            </div>
        </div>
    </div>
</template>
<script>
import api from '../../api/index';
import bus from '../common/bus';
import pageUtil from '../../utils/page';

export default {
    data() {
        return {
            collapse: false,
            fullscreen: false,
            name: 'linxin',
            message: 2,
            activeIndex: "1",
            permissions: undefined
        };
    },
    computed: {
        username() {
            let username = localStorage.getItem('ms_username');
            return username ? username : this.name;
        },
        rolename() {
            if (this.username === 'admin') {
                return '超级管理员';
            }
            let roleName = window.localStorage.getItem('role_name');
            return roleName;
        }
    },
    created() {
        const role = localStorage.getItem('ms_username');
        // var permsObj = window.localStorage.getItem('permissions');
        // if (role !== 'admin' && permsObj) {
        //     var perms = JSON.parse(permsObj);
        //     this.permissions = perms;
        // }
        var permsObj = window.localStorage.getItem('routes');
        if (permsObj) {
            var perms = JSON.parse(permsObj);
            this.permissions = perms;
        }
    },
    methods: {
        applyForPermission(page) {
            if (this.permissions === undefined) {
                return true;
            }
            else {
                var arrP = this.permissions.filter(e => {
                    return e.name === page;
                });
                if (arrP.length > 0) {
                    return true;
                }
                else {
                    return false;
                }
            }
        },
        gotoPage(page) {
            pageUtil.gotoPage(this, page);
        },
        handleSelect() {

        },
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                api.logout().then(res => {
                    localStorage.removeItem('ms_username');
                    this.$router.push('/login');
                }).catch(err => {
                    this.$message.error('账号或密码错误');
                })
            }
        },
        // 侧边栏折叠
        collapseChage() {
            this.collapse = !this.collapse;
            bus.$emit('collapse', this.collapse);
        },
        // 全屏事件
        handleFullScreen() {
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        }
    },
    mounted() {
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
    }
};
</script>
<style scoped>

</style>
